package com.work.order.service;

import com.alibaba.fastjson.JSONObject;
import com.work.order.model.Order;
import com.work.order.repository.OrderDAO;
import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service
public class TccOrderServiceImpl implements TccOrderService{

    @Resource
    private OrderDAO orderDAO;

    /*@Transactional
    @Override
    public boolean prepare(BusinessActionContext context, String userId, String commodityCode, Integer count, String orderNo, BigDecimal orderMoney) {
        //下单
        Order order = new Order()
                .setUserId(userId)
                .setCommodityCode(commodityCode)
                .setCount(count)
                .setOrderNo(orderNo)
                .setMoney(orderMoney);
        //状态：0
        orderDAO.insert(order);

        context.getActionContext().put("test","object");
        //TODO 待验证，prepare阶段，存到 context 的数据，能否在 下面方法中拿到 -- 拿不到
        log.info("########### 打印，XID = " + context.getXid());
        log.info("################ order prepare ################");
        return true;
    }*/

    @Transactional
    @Override
    public boolean prepare(BusinessActionContext context, Order order) {
        //下单
        //状态：0
        orderDAO.insert(order);

        context.getActionContext().put("test","object");
        // TODO 待验证，prepare阶段，存到 order 的数据，能否在 下面方法中拿到 -- 不能
        HashMap<String, Object> map = new HashMap<>(1);
        map.put("test","测试传递数据");
        order.setBusContext(map);

        log.info("########### 打印，XID = " + context.getXid());
        log.info("################ order prepare ################");
        return true;
    }

    /**
     * 提交
     * @param context
     */
    @Transactional
    @Override
    public boolean commit(BusinessActionContext context) {
        Map<String, Object> actionContext = context.getActionContext();
        JSONObject jsonObject = (JSONObject)actionContext.get("order");
        Order order1 = jsonObject.toJavaObject(Order.class);

        //改订单状态
        Order order = orderDAO.findByOrderNo(order1.getOrderNo());
        order.setStatus(1);
        orderDAO.updateById(order);

        log.info("################ order commit ################");
        return true;
    }

    /**
     * 回滚
     * @param context
     */
    @Transactional
    @Override
    public boolean rollback(BusinessActionContext context) {
        Map<String, Object> actionContext = context.getActionContext();
        String orderNo= (String) actionContext.get("orderNo");
        //删除订单
        Order order = orderDAO.findByOrderNo(orderNo);
        if (ObjectUtils.isNotEmpty(order)){
            orderDAO.deleteById(order.getId());
        }

        log.info("################ order rollback ################");
        return true;
    }
}
